﻿/********************************************
砂岩统计 by 曹彬 20140518
********************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using XMK.Framework.DataAccess;
using System.Text;
using XMK.Tadong.Biz;
using System.IO;
using System.Data.OleDb;
using XMK.Tadong.Model;
using System.Drawing;

namespace XMK.Tadong.Web.TJ
{
    public partial class TJ_SYTJ : System.Web.UI.Page
    {

        #region  构造函数

        public TJ_SYTJ()
        {
            //插入日志
            //string IP = Request.ServerVariables["REMOTE_ADDR"].Equals("::1") ? "127.0.0.1" : Request.ServerVariables["REMOTE_ADDR"];
            //XMK.Tadong.Model.Cond.Base_PageLog pageLogModel = new Model.Cond.Base_PageLog();
            //pageLogModel.pageName = "统计分析";
            //pageLogModel.ip = IP;
            //XMKLogBiz.WriteLogScanPage(pageLogModel);
        }

        #endregion  构造函数

        #region 变量


        //private tj_webservice.tj_service serclient;
        string TJDMStr, CWStr, UserID, JHStr, MAXStr, MINStr;
        double DingJSDfl, DIJSDfl, HDfl, MAXfl, MINfl, AVEfl, LJZfl, SDBfl;
        protected System.Web.UI.WebControls.Button NYButton;

        #endregion 变量

        #region 事件处理程序

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if (!string.IsNullOrEmpty(this.Request.QueryString["JH"].ToString()))
                //    inputJH.Text = this.Request.QueryString["JH"].ToString();
                //UserID = Session["user_id"].ToString();
                //BindSY();
                //BindCM();
            }
        }

        #endregion 事件处理程序

        #region 函数集

        //绑定砂岩名称
        public void BindSY()
        {
            LJT_YXPMBLL bll = new LJT_YXPMBLL();
            DataTable dt = bll.GetModelList();
            ckboxlist.DataSource = dt;
            ckboxlist.DataTextField = "YXMC";
            ckboxlist.DataValueField = "ID";
            ckboxlist.DataBind();
        }

        ///// <summary>
        ///// 绑定层名
        ///// </summary>
        //public void BindCM()
        //{
        //    DataTable dt = new DataTable();
        //    dt.Columns.Add("ID");
        //    dt.Columns.Add("NAME");
        //    DataRow dr;
        //    dr = dt.NewRow();
        //    dr["ID"] = "0";
        //    dr["NAME"] = "请选择";
        //    dt.Rows.Add(dr);
        //    dr = dt.NewRow();
        //    dr["ID"] = "1";
        //    dr["NAME"] = "高台子";
        //    dt.Rows.Add(dr);
        //    ddlycname.DataSource = dt;
        //    ddlycname.DataTextField = "NAME";
        //    ddlycname.DataValueField = "ID";
        //    ddlycname.DataBind();
        //}

        #endregion 函数集
        //#region 事件处理程序

        //protected void Page_Load(object sender, EventArgs e)
        //{
        //    UserID = Session["user_id"].ToString();
        //    //serclient = new tj_webservice.tj_service();


        //    //MacStr = Session["MacAddress"].ToString();

        //    //判断是否显示下载按钮
        //    //if (DownRight == "0")
        //    //{
        //    //    btnDownLoad.Visible = false;
        //    //}
        //    if (!IsPostBack)
        //    {
        //        string strJHList = "";
        //        string strSql = "select distinct jh  from  giswell where jh in (select jh from zhuluoji.yqcjd)";
        //        DbCmdHelper cmd = new DbCmdHelper(dbProvider, strSql.ToString());
        //        cmd.DbCmdInfo.CommandType = CommandType.Text;
        //        DataSet ds = DbHelper.ExecFullDataSet(dbProvider, cmd.DbCmdInfo);
        //        int numcount = ds.Tables[0].Rows.Count;
        //        if (numcount > 20)
        //            numcount = 20;  //如果满足条件的井太多，后面构造的查询语句会过长，导致错误，所以这里限制只显示２０口井

        //        for (int i = 0; i < numcount; i++)
        //        {
        //            strJHList += "," + ds.Tables[0].Rows[i][0].ToString();
        //        }
        //        if (strJHList != "")
        //            strJHList = strJHList.Substring(1, strJHList.Length - 1);
        //        inputJH.Text = strJHList;

        //    }
        //}

        /// <summary>
        /// 统计单口井的砂岩，按井号统计
        /// </summary>
        /// 1
        /// 
        protected void SYButton_Click(object sender, System.EventArgs e)
        {
            ChartPeople.Visible = true;
            string dt = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss"); //DateTime.Now.ToShortDateString();
            TJDMStr = UserID + "/" + dt + "/" + "砂岩(录井图)";
            usertb.Text = TJDMStr;


            string str, strsy;
            string strresult = "";
            MAXStr = MINStr = "";
            int num = ckboxlist.Items.Count;
            JHStr = tb.Text;
            XMK_A_Z_SY03BLL xmk1Bll = new XMK_A_Z_SY03BLL();
            string delestr = "select count(*) from xmk_a_z_sy03 where JH in (" + JHStr + ")";
            if (xmk1Bll.IsExist(delestr))
            {
                string deletestr = "delete from xmk_a_z_sy03 where JH in (" + JHStr + ")";
                xmk1Bll.Delete(deletestr);
            }
            for (int i = 0; i < num; i++)
            {
                if (ckboxlist.Items[i].Selected)
                    strresult += "'" + ckboxlist.Items[i].Text + "'" + ",";
            }

            if (strresult.Length == 0)
            {

                MessageLabel.Text = "请选择统计类型！";
                ChartPeople.Visible = false;
            }
            else
            {
                strresult = strresult.Substring(0, strresult.Length - 1);
                try
                {
                    YQCJDBLL bll = new YQCJDBLL();
                    string strWhere = @" and JH in (" + JHStr + ")";

                    DataSet ds1 = bll.GetData(strWhere);
                    int number = ds1.Tables[0].Rows.Count;//记录集的数目1891
                    if (number == 0)
                    {
                        MessageLabel.Text = "没有数据！";
                        ChartPeople.Visible = false;
                        return;
                    }
                    else
                    {
                        for (int i = 0; i < number; i++)//对应的是每一层
                        {
                            string hjyjh = ds1.Tables[0].Rows[i]["JH"].ToString();
                            double dlx = 0, dly = 0;
                            //string strzb = "select X,Y	from GISWELL where JH='" + hjyjh + "'";
                            //DbCmdHelper cmd1 = new DbCmdHelper(dbProvider, strzb.ToString());
                            //cmd1.DbCmdInfo.CommandType = CommandType.Text;
                            //DataSet dszb = DbHelper.ExecFullDataSet(dbProvider, cmd.DbCmdInfo);
                            ////DataSet dszb = serclient.GetDataSet(strzb);
                            //dlx = Convert.ToDouble(dszb.Tables[0].Rows[0][0].ToString());
                            //dly = Convert.ToDouble(dszb.Tables[0].Rows[0][1].ToString());
                            MAXfl = MINfl = AVEfl = LJZfl = 0;
                            JHStr = JHStr;
                            DingJSDfl = Convert.ToDouble(ds1.Tables[0].Rows[i]["DJSD1"].ToString());
                            DIJSDfl = Convert.ToDouble(ds1.Tables[0].Rows[i]["DJSD2"].ToString());
                            CWStr = ds1.Tables[0].Rows[i]["YCZMC"].ToString();
                            HDfl = DIJSDfl - DingJSDfl;
                            
                            string nodatajh = "";
                            

                            LJT_YXPMBLL ljtBLL = new LJT_YXPMBLL();
                            string strWhere1 = @" and (JH='" + hjyjh + "')and (DBSD1 >=" + DingJSDfl + ")and  (DBSD<=" + DIJSDfl + ") and (YXMC in(" + strresult + "))";

                            DataSet ds2 = ljtBLL.GetData(strWhere1);
                            int number2 = ds2.Tables[0].Rows.Count;
                            if (number2 == 0)
                            {

                                nodatajh = nodatajh + hjyjh;
                                MessageLabel.Text = nodatajh + "没有数据！";
                                ChartPeople.Visible = false;
                                //return;
                            }
                            else
                            {

                                int j;
                                LJZfl = 0;
                                double maxds1 = 0, maxds2 = 0, minds1 = 0, minds2 = 0;
                                for (j = 0; j < number2; j++)//比较大小取出最大，最小，平均值
                                {
                                    //string JHSTR1;

                                    double Dingj, DIj;

                                    // JHSTR1=ds2.item[j][0];
                                    Dingj = Convert.ToDouble(ds2.Tables[0].Rows[j]["DBSD"].ToString());
                                    DIj = Convert.ToDouble(ds2.Tables[0].Rows[j]["DBSD1"].ToString());
                                    if (j == 0)
                                    {
                                        maxds1 = minds1 = Dingj;
                                        maxds2 = minds2 = DIj;
                                        MAXfl = MINfl = AVEfl = LJZfl = DIj - Dingj;

                                    }
                                    else
                                    {
                                        if ((DIj - Dingj) >= MAXfl)
                                        {
                                            MAXfl = Convert.ToDouble(DIj - Dingj);
                                            maxds1 = Dingj;
                                            maxds2 = DIj;
                                        }
                                        if ((DIj - Dingj) <= MINfl)
                                        {
                                            MINfl = Convert.ToDouble(DIj - Dingj);
                                            minds1 = Dingj;
                                            minds2 = DIj;
                                        }
                                        LJZfl = LJZfl + (DIj - Dingj);
                                    }
                                    AVEfl = LJZfl / (j + 1);
                                }
                                SDBfl = LJZfl / HDfl;
                                MAXStr = Math.Round(MAXfl, 2) + selectCH(hjyjh, maxds1, maxds2);
                                MINStr = Math.Round(MINfl, 2) + selectCH(hjyjh, minds1, minds2);
                                //selectCH(stringjh,minds1,minds2);
                                HDfl = Math.Round(HDfl, 2);
                                AVEfl = Math.Round(AVEfl, 2);
                                LJZfl = Math.Round(LJZfl, 2);
                                SDBfl = Math.Round(SDBfl, 2);
                                XMK_A_Z_SY03BLL xmkBll = new XMK_A_Z_SY03BLL();
                                XMK_A_Z_SY03Info model = new XMK_A_Z_SY03Info();
                                model.JH = hjyjh;
                                model.DINGJSD = DingJSDfl;
                                model.DIJSD = DIJSDfl;
                                model.HD = HDfl;
                                model.AVER = AVEfl;
                                model.LJHD = LJZfl;
                                model.SDB = SDBfl;
                                model.CW = CWStr;
                                model.DLX = dlx;
                                model.DLY = dly;
                                model.DJDM = usertb.Text;
                                bool bl = xmkBll.Add(model);
                                if (bl == false)
                                {
                                    MessageLabel.Text = nodatajh + "没有成功统计！";
                                    ChartPeople.Visible = false;
                                    //return;
                                }
                            }
                        }
                    }
                    Session["hjyjhstr"] = JHStr;
                    //JHStr=ddlHaveDataJH.SelectedItem.Value;
                    string strds = "select * from xmk_a_z_sy03 where DJDM='" + usertb.Text + "'and JH in (" + JHStr + ") order by DINGJSD asc";
                    btnDownLoad.Enabled = true;
                    XMK_A_Z_SY03BLL cmkBll = new XMK_A_Z_SY03BLL();
                    string strWhere2 = @" and DJDM='" + usertb.Text + "'and JH in (" + JHStr + ") order by DINGJSD asc";

                    DataSet dataset = cmkBll.GetData(strWhere2);
                    if (dataset.Tables[0].Rows.Count == 0)
                    {
                        MessageLabel.Text = "没有统计数据！";
                        ChartPeople.Visible = false;
                    }
                    else
                    {
                        TJDataGrid.DataSource = dataset;
                        TJDataGrid.DataBind();
                        string sql = "";
                        //sql = "select CW,DINGJSD,DIJSD,HD,AVER,LJHD,SDB from XMK_A_Z_SY03 WHERE JH IN(" + tb.Text + ") order by dingjsd";
                        sql = @" and JH IN(" + tb.Text + ") order by dingjsd";
                        BindPeople(sql);

                    }
                    
                }
                catch (Exception ee)
                {
                    throw ee;
                }
            }

        }

        //绑定chart图表
        private void BindPeople(string SQL)
        {
            
            XMK_A_Z_SY03BLL bll = new XMK_A_Z_SY03BLL();
            DataSet ds = bll.GetData(SQL);
            DataTable dt = ds.Tables[0];
            ChartPeople.DataSource = dt;
            ChartPeople.Series["DINGJSD"].XValueMember = "JH";
            ChartPeople.Series["DINGJSD"].YValueMembers = "DINGJSD";
            ChartPeople.Series["DIJSD"].YValueMembers = "DIJSD";
            ChartPeople.Series["HD"].YValueMembers = "HD";
            ChartPeople.Series["AVER"].YValueMembers = "AVER";
            ChartPeople.Series["LJHD"].YValueMembers = "LJHD";
            ChartPeople.Series["SDB"].YValueMembers = "SDB";
            ChartPeople.DataBind();//绑定数据";
        }

        //数据下载
        protected void btnDownLoad_Click(object sender, System.EventArgs e)
        {
            try
            {
                if (TJDataGrid.Items.Count == 0)
                {
                    throw new Exception("没有数据不能导出！");
                }
                string FileName = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss");
                FileName = System.Web.HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8) + ".xls";
                ExportDataGrid(TJDataGrid, "application/ms-excel", FileName);
            }
            catch (Exception ex)
            {
                Response.Write("<script language =javascript >alert('" + ex.Message + "');</script>");
                return;
            }
        }


        /// <summary>从DataGrid导出指定类型的文件</summary>
        /// <summary>Super 2005/8/18</summary>
        /// <param name="ctl">DataGrid的ID</param>
        /// <param name="FileType">导出文件类型//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ;//application/ms-excel</param>
        /// <param name="FileName">导出的文件名</param>
        /// <returns>文件</returns>
        public static void ExportDataGrid(System.Web.UI.Control ctl, string FileType, string FileName)
        {
            HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);
            HttpContext.Current.Response.Charset = "gb2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            HttpContext.Current.Response.ContentType
            = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword ;//application/ms-excel
            ctl.Page.EnableViewState = false;
            System.IO.StringWriter tw = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
            ctl.RenderControl(hw);
            HttpContext.Current.Response.Write(tw.ToString());
            HttpContext.Current.Response.End();
        } 

        /// <summary>
        /// 根据YCZMC统计某一YCZMC的所有井的砂岩信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void ButtonSYCW_Click(object sender, System.EventArgs e)
        {
            TJDataGrid.DataSource = null;
            TJDataGrid.DataBind();
            ChartPeople.Visible = true;
            string dt = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss"); //DateTime.Now.ToShortDateString();
            TJDMStr = UserID + "/" + dt + "/" + "砂岩(录井图)";
            usertb.Text = TJDMStr;

            string str, strsy;
            string strresult = "";
            int num = ckboxlist.Items.Count;

            if (checkbase.Checked == true)
            {
                JHStr = ddlycname.SelectedItem.Text;
            }
            else
            {
                JHStr = yc_nametb.Text;
            }
            XMK_A_Z_SY03BLL xmk1Bll = new XMK_A_Z_SY03BLL();
            string delestr = "select count(*) from xmk_a_z_sy03 where CW='" + JHStr + "'";
            if (xmk1Bll.IsExist(delestr))
            {
                string deletestr = "delete from xmk_a_z_sy03 where CW='" + JHStr + "'";
                xmk1Bll.Delete(deletestr);
            }
            MAXStr = "";
            MINStr = "";
            if (JHStr == "请选择")
            {
                MessageLabel.Text = "请选择正确的YCZMC！";
                ChartPeople.Visible = false;
                return;
            }
            else
            {
                MessageLabel.Text = "";
            }
            for (int i = 0; i < num; i++)
            {
                if (ckboxlist.Items[i].Selected)
                    strresult += "'" + ckboxlist.Items[i].Text + "'" + ",";
            }
            if (strresult.Length == 0)
            {
                MessageLabel.Text = "请选择统计类型！";
                ChartPeople.Visible = false;
            }
            else
            {
                MessageLabel.Text = "";
                strresult = strresult.Substring(0, strresult.Length - 1);
                try
                {
                    if (checkbase.Checked == true)
                    {
                        //str = "select JH,DJSD1,DJSD2,YCZMC,HD from zhuluoji.YQCJD where (YCZMC ='" + JHStr + "')  and (JH in(" + tb.Text + ")) order by DJSD1 asc";
                        YQCJDBLL bll = new YQCJDBLL();
                        string strWhere = @" and (YCZMC ='" + JHStr + "')  and (JH in(" + tb.Text + ")) order by DJSD1 asc";


                        DataSet ds1 = bll.GetData(strWhere);//得到数据集
                        int number = ds1.Tables[0].Rows.Count;//记录集的数目1891					
                        if (number == 0)
                        {
                            MessageLabel.Text = "没有统计数据！";
                            ChartPeople.Visible = false;
                        }
                        else
                        {
                            MessageLabel.Text = "";
                            for (int i = 0; i < number; i++)//对应的是每一层
                            {
                                string hjyjh = ds1.Tables[0].Rows[i]["JH"].ToString();
                                double dlx = 0;
                                double dly = 0;
                                //string strzb = "select X,Y	from GISWELL where JH='" + hjyjh + "'";
                                //DataSet dszb = serclient.GetDataSet(strzb);
                                //dlx = Convert.ToDouble(dszb.Tables[0].Rows[0][0].ToString());
                                //dly = Convert.ToDouble(dszb.Tables[0].Rows[0][1].ToString());
                                MAXfl = MINfl = AVEfl = LJZfl = 0;
                                JHStr = JHStr;
                                DingJSDfl = Convert.ToDouble(ds1.Tables[0].Rows[i]["DJSD1"].ToString());
                                DIJSDfl = Convert.ToDouble(ds1.Tables[0].Rows[i]["DJSD2"].ToString());
                                CWStr = ds1.Tables[0].Rows[i]["YCZMC"].ToString();
                                HDfl = DIJSDfl - DingJSDfl;
                                //strny="select * from  LJT_YXPM where (JH='"+JHStr+"')and (DJSD1 >='"+DingJSDfl+"')and  (DJSD2<='"+DIJSDfl+"') and (YXMC like '%泥岩%')";
                                //strsy = "select JH,DBSD1,DBSD from  zhuluoji.ljt_YXPM where (JH='" + hjyjh + "')and (DBSD1 >=" + DingJSDfl + ")and  (DBSD<=" + DIJSDfl + ") and (YXMC in(" + strresult + "))";
                                LJT_YXPMBLL ljtBLL = new LJT_YXPMBLL();
                                string strWhere1 = @" and (JH='" + hjyjh + "')and (DBSD1 >=" + DingJSDfl + ")and  (DBSD<=" + DIJSDfl + ") and (YXMC in(" + strresult + "))";
                                DataSet ds2 = ljtBLL.GetData(strWhere1);
                                int number2 = ds2.Tables[0].Rows.Count;
                                if (number2 == 0)
                                    MessageLabel.Text = "";
                                else
                                {
                                    int j;
                                    LJZfl = 0;
                                    double maxds1 = 0, maxds2 = 0, minds1 = 0, minds2 = 0;
                                    for (j = 0; j < number2; j++)//比较大小取出最大，最小，平均值
                                    {
                                        double Dingj, DIj;
                                        Dingj = Convert.ToDouble(ds2.Tables[0].Rows[j]["DJSD1"].ToString());
                                        DIj = Convert.ToDouble(ds2.Tables[0].Rows[j]["DJSD2"].ToString());
                                        if (j == 0)
                                        {
                                            maxds1 = minds1 = Dingj;
                                            maxds2 = minds2 = DIj;
                                            MAXfl = MINfl = AVEfl = LJZfl = DIj - Dingj;

                                        }
                                        else
                                        {
                                            if ((DIj - Dingj) >= MAXfl)
                                            {
                                                MAXfl = Convert.ToDouble(DIj - Dingj);
                                                maxds1 = Dingj;
                                                maxds2 = DIj;
                                            }
                                            if ((DIj - Dingj) <= MINfl)
                                            {
                                                MINfl = Convert.ToDouble(DIj - Dingj);
                                                minds1 = Dingj;
                                                minds2 = DIj;
                                            }
                                            LJZfl = LJZfl + (DIj - Dingj);
                                        }
                                        AVEfl = LJZfl / (j + 1);
                                    }
                                    SDBfl = LJZfl / HDfl;
                                    MAXStr = Math.Round(MAXfl, 2) + selectCH(hjyjh, maxds1, maxds2);
                                    MINStr = Math.Round(MINfl, 2) + selectCH(hjyjh, minds1, minds2);
                                    HDfl = Math.Round(HDfl, 2);
                                    AVEfl = Math.Round(AVEfl, 2);
                                    LJZfl = Math.Round(LJZfl, 2);
                                    SDBfl = Math.Round(SDBfl, 2);
                                    XMK_A_Z_SY03BLL xmkBll = new XMK_A_Z_SY03BLL();
                                    XMK_A_Z_SY03Info model = new XMK_A_Z_SY03Info();
                                    model.JH = hjyjh;
                                    model.DINGJSD = DingJSDfl;
                                    model.DIJSD = DIJSDfl;
                                    model.HD = HDfl;
                                    model.AVER = AVEfl;
                                    model.LJHD = LJZfl;
                                    model.SDB = SDBfl;
                                    model.CW = CWStr;
                                    model.DLX = dlx;
                                    model.DLY = dly;
                                    model.DJDM = usertb.Text;
                                    bool bl = xmkBll.Add(model);
                                    if (bl == false)
                                    {
                                        MessageLabel.Text = "没有成功统计！";
                                        ChartPeople.Visible = false;
                                        return;
                                    }
                                }
                            }
                        }
                    }
                    else
                    {
                        string hjyjh = "";
                        double dlx = 0, dly = 0;
                        if (yc_nametb.Text.Trim() == "" || starttb.Text.Trim() == "" || endtb.Text.Trim() == "")
                        {
                            MessageLabel.Text = "请填写层名以及起始井深！";
                            ChartPeople.Visible = false;
                            return;
                        }
                        //string strzb = "select JH,X,Y	from GISWELL where JH in (" + tb.Text + ") ";//and YC='"+yc_nametb.Text +"'";
                        //DataSet dszb = serclient.GetDataSet(strzb);
                        //for (int i = 0; i < dszb.Tables[0].Rows.Count; i++)
                        //{
                        hjyjh = JHStr;
                            //dlx = Convert.ToDouble(dszb.Tables[0].Rows[i][1].ToString());
                            //dly = Convert.ToDouble(dszb.Tables[0].Rows[i][2].ToString());
                            MAXfl = MINfl = AVEfl = LJZfl = 0;
                            JHStr = JHStr;
                            DingJSDfl = Convert.ToDouble(starttb.Text);
                            DIJSDfl = Convert.ToDouble(endtb.Text);
                            CWStr = yc_nametb.Text;
                            HDfl = DIJSDfl - DingJSDfl;

                            strsy = "select JH,DBSD1,DBSD from  zhuluoji.ljt_YXPM where (JH='" + hjyjh + "')and (DBSD1 >=" + DingJSDfl + ")and  (DBSD<=" + DIJSDfl + ") and (YXMC in(" + strresult + "))";
                            string strWhere = @" and (JH='" + hjyjh + "')and (DBSD1 >=" + DingJSDfl + ")and  (DBSD<=" + DIJSDfl + ") and (YXMC in(" + strresult + "))";
                            LJT_YXPMBLL ljtbll = new LJT_YXPMBLL();
                            DataSet ds2 = ljtbll.GetData(strWhere);
                            int number2 = ds2.Tables[0].Rows.Count;
                            if (number2 == 0)
                                MessageLabel.Text = MessageLabel.Text;
                            else
                            {
                                int j;
                                LJZfl = 0;
                                double maxds1 = 0, maxds2 = 0, minds1 = 0, minds2 = 0;
                                for (j = 0; j < number2; j++)//比较大小取出最大，最小，平均值
                                {
                                    double Dingj, DIj;
                                    Dingj = Convert.ToDouble(ds2.Tables[0].Rows[j]["DJSD1"].ToString());
                                    DIj = Convert.ToDouble(ds2.Tables[0].Rows[j]["DJSD2"].ToString());
                                    if (j == 0)
                                    {
                                        maxds1 = minds1 = Dingj;
                                        maxds2 = minds2 = DIj;
                                        MAXfl = MINfl = AVEfl = LJZfl = DIj - Dingj;
                                    }
                                    else
                                    {
                                        if ((DIj - Dingj) >= MAXfl)
                                        {
                                            MAXfl = Convert.ToDouble(DIj - Dingj);
                                            maxds1 = Dingj;
                                            maxds2 = DIj;
                                        }
                                        if ((DIj - Dingj) <= MINfl)
                                        {
                                            MINfl = Convert.ToDouble(DIj - Dingj);
                                            minds1 = Dingj;
                                            minds2 = DIj;
                                        }
                                        LJZfl = LJZfl + (DIj - Dingj);
                                    }
                                    AVEfl = LJZfl / (j + 1);
                                }
                                SDBfl = LJZfl / HDfl;
                                MAXStr = Math.Round(MAXfl, 2) + selectCH(hjyjh, maxds1, maxds2);
                                MINStr = Math.Round(MINfl, 2) + selectCH(hjyjh, minds1, minds2);
                                HDfl = Math.Round(HDfl, 2);
                                AVEfl = Math.Round(AVEfl, 2);
                                LJZfl = Math.Round(LJZfl, 2);
                                SDBfl = Math.Round(SDBfl, 2);

                                XMK_A_Z_SY03BLL xmkBll = new XMK_A_Z_SY03BLL();
                                XMK_A_Z_SY03Info model = new XMK_A_Z_SY03Info();
                                model.JH = hjyjh;
                                model.DINGJSD = DingJSDfl;
                                model.DIJSD = DIJSDfl;
                                model.HD = HDfl;
                                model.AVER = AVEfl;
                                model.LJHD = LJZfl;
                                model.SDB = SDBfl;
                                model.CW = CWStr;
                                model.DLX = dlx;
                                model.DLY = dly;
                                model.DJDM = usertb.Text;
                                bool bl = xmkBll.Add(model);
                                if (bl == false)
                                {
                                    MessageLabel.Text = "没有成功统计！";
                                    ChartPeople.Visible = false;
                                    return;
                                }
                            }
                        //}

                    }
                    string strds = "select * from xmk_a_z_sy03 where DJDM='" + usertb.Text + "'and JH in (" + tb.Text + ") and CW='" + JHStr + "' order by DINGJSD asc";
                    btnDownLoad.Enabled = true;
                    XMK_A_Z_SY03BLL cmkBll = new XMK_A_Z_SY03BLL();
                    string strWhere2 = @" and DJDM='" + usertb.Text + "'and JH in (" + JHStr + ") order by DINGJSD asc";
                    DataSet dataset = cmkBll.GetData(strWhere2);
                    if (dataset.Tables[0].Rows.Count == 0)
                    {
                        MessageLabel.Text = "没有统计数据！";
                        ChartPeople.Visible = false;
                    }
                    else
                    {
                        TJDataGrid.DataSource = dataset;
                        TJDataGrid.DataBind();
                        string sql = "";
                        if (checkbase.Checked == true)
                        {
                            //sql = "select jh,DINGJSD,DIJSD,HD,AVER,LJHD,SDB from xmk_a_z_sy03 where cw='" + ddlycname.SelectedItem.Text + "' order by dingjsd";
                            sql = @" and cw='" + ddlycname.SelectedItem.Text + "' order by dingjsd";
                        }
                        else
                        {
                            //sql = "select jh,DINGJSD,DIJSD,HD,AVER,LJHD,SDB from xmk_a_z_sy03 where cw='" + yc_nametb.Text + "' order by dingjsd";
                            sql = @" and  cw='" + yc_nametb.Text + "' order by dingjsd";
                        }

                        BindPeople(sql);
                    }
                }
                catch (Exception ee)
                {
                    throw ee;
                }
            }
        }

        //自定义分层
        protected void checkperson_CheckedChanged(object sender, System.EventArgs e)
        {
            if (checkperson.Checked == true)
            {
                yc_namelabel.Enabled = true;
                yc_nametb.Enabled = true;
                SELabel.Enabled = true;
                starttb.Enabled = true;
                endtb.Enabled = true;
                ycname1.Enabled = false;
                ddlycname.Enabled = false;
                ButtonSYCW.Enabled = true;
                SYButton.Enabled = false;
            }
        }

        //数据库分层
        protected void checkbase_CheckedChanged(object sender, System.EventArgs e)
        {
            TJDataGrid.DataSource = null;
            TJDataGrid.DataBind();
            if (checkbase.Checked == true)
            {
                yc_namelabel.Enabled = false;
                yc_nametb.Enabled = false;
                SELabel.Enabled = false;
                starttb.Enabled = false;
                endtb.Enabled = false;
                ycname1.Enabled = true;
                ddlycname.Enabled = true;
            }
        }

        //井号提交点击事件
        protected void commitJH_Click(object sender, System.EventArgs e)
        {
            ckboxlist.Items.Clear();
            SYButton.Enabled = true;
            ButtonSYCW.Enabled = false;
            string strjh = inputJH.Text;
            string strcw = "";
            strjh = strjh.Replace("，", ",");
            strjh = "'" + strjh.Replace(",", "','") + "'";
            tb.Text = strjh;
            strcw = GetHaveCW(strjh);
            FillListFromPara(strcw, ddlycname);
            SYButton.Visible = true;
            string strWhere = @" and (YXMC like '%砂岩%') and (JH in (" + tb.Text + "))";
            LJT_YXPMBLL bll = new LJT_YXPMBLL();
            DataSet ds = bll.GetData(strWhere);
            DataTable dt = ds.Tables[0];
            int num = dt.Rows.Count;
            for (int i = 0; i < num; i++)
            {
                ListItem li = new ListItem();
                li.Value = dt.Rows[i]["YXMC"].ToString();
                li.Text = dt.Rows[i]["YXMC"].ToString();
                ckboxlist.Items.Add(li);
            }
            //ckboxlist.RepeatColumns =num;
            Checkall.Enabled = true;
            if (Checkall.Checked == true)
            {
                for (int i = 0; i < num; i++)
                {
                    ckboxlist.Items[i].Selected = true;
                }
            }
        }

        protected void ddlycname_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            ButtonSYCW.Enabled = true;
            SYButton.Enabled = false;
        }


        //#endregion 事件处理程序

        #region 函数集

        private string GetHaveCW(string stringjh)
        {

            string strReturn = "";
            string str = "select distinct(YCZMC) from YQCJD where JH in(" + stringjh + ") ";
            string strWhere = @" and JH in(" + stringjh + ")";
            YQCJDBLL bll = new YQCJDBLL();
            DataSet ds = bll.GetData(strWhere);


            int numcount = ds.Tables[0].Rows.Count;
            for (int i = 0; i < numcount; i++)
            {
                strReturn += "," + ds.Tables[0].Rows[i][0].ToString();
            }
            if (strReturn != "")
                strReturn = strReturn.Substring(1, strReturn.Length - 1);
            return strReturn;
        }

        private void FillListFromPara(string p_JH, DropDownList p_ddl)
        {
            p_ddl.Items.Clear();
            p_ddl.Items.Add("请选择");
            string[] split = null;
            split = p_JH.Split(',');
            foreach (string s in split)
            {
                p_ddl.Items.Add(s);
            }

        }//private void FillListFromPara

       

        //全选砂岩分类
        protected void Checkall_CheckedChanged(object sender, System.EventArgs e)
        {
            int num = ckboxlist.Items.Count;
            if (Checkall.Checked == true)
            {
                for (int i = 0; i < num; i++)
                {
                    ckboxlist.Items[i].Selected = true;
                }
            }
            else
            {
                for (int i = 0; i < num; i++)
                {
                    ckboxlist.Items[i].Selected = false;
                }
            }
        }

        /// <summary>
        /// 选出最大值,最小值的JH
        /// </summary>
        /// <param name="jhstr"></param>
        /// <param name="ds1"></param>
        /// <param name="ds2"></param>
        /// <returns></returns>
        private string selectCH(string jhstr, double ds1, double ds2)
        {
            string str = "select CH from AC01 where DJSD1>=" + ds1 + " and DJSD2<=" + ds2 + " and JH='" + jhstr + "'";
            //DataSet ds = serclient.GetDataSet(str);

            //int number = ds.Tables[0].Rows.Count;
            int number = 0;
            if (number == 0)
            {
                ds1 = Math.Round(ds1, 2);
                ds2 = Math.Round(ds2, 2);
                string s1 = "(" + ds1 + "," + ds2 + ")";
                return s1;
            }
            else
            {
                string s2 = "";
                //for (int i = 0; i < number; i++)
                //{
                //    if (ds.Tables[0].Rows[i][0].ToString() != "")
                //    {
                //        s2 = s2 + "," + ds.Tables[0].Rows[i][0].ToString();
                //        s2 = "(" + s2.Substring(1, s2.Length - 1) + ")";
                //    }
                //}
                s2 = s2 + "," + "1";
                s2 = "(" + s2.Substring(1, s2.Length - 1) + ")";
                return s2;
            }
        }


        #endregion 函数集
    }
}